using System ;
using System.Web.Management ;
using System.Runtime.Serialization ;

namespace OpenLightGroup.Common.ApplicationServices
{
    /// <summary>
    /// Represents a standard exception that needs to be logged.
    /// </summary>
    /// <remarks>
    /// Both constructors shown below are needed so that the WebEvent API 
    /// can see that this type should be handled as an WebErrorEvent
    /// </remarks>
    public class ErrorEvent : WebErrorEvent
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorEvent" /> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="eventSource">The event source.</param>
        /// <param name="e">The exception.</param>
        protected ErrorEvent ( string message,
                              object eventSource,
                              Exception e )
        : base ( message, eventSource, 100000, e )
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ErrorEvent" /> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="eventSource">The event source.</param>
        /// <param name="eventDetailCode">The event detail code.</param>
        /// <param name="e">The exception.</param>
        protected ErrorEvent ( string message,
                              object eventSource,
                              int eventDetailCode,
                              Exception e )
        : base ( message, eventSource, 100000, eventDetailCode, e )
        {
        }

        /// <summary>
        /// Formats the custom event details when being stored by the provider.
        /// </summary>
        /// <param name="formatter">The formatter.</param>
        public override void FormatCustomEventDetails ( WebEventFormatter formatter )
        {
            base.FormatCustomEventDetails ( formatter ) ;

            if ( this.ErrorException != null )
            {
                formatter.AppendLine ( "Stack Trace Information:" ) ;

                var innerException = this.ErrorException ;
              
                for ( int i = 0 ; ( innerException != null ) && ( i <= 2 ) ; i++ )
                {
                    formatter.IndentationLevel++ ;
                  
                    formatter.AppendLine (
                        String.Format ( "Exception Type: {0}", innerException.GetType ( ) ) ) ;

                    formatter.AppendLine (
                        String.Format ( "Message: {0}", innerException.Message ) ) ;

                    formatter.AppendLine ( "Stack Trace:" ) ;
                    formatter.AppendLine ( innerException.StackTrace ) ;
                                  
                    formatter.IndentationLevel-- ;

                    innerException = innerException.InnerException ;
                }
            }
        }
    }
}